﻿var code = "";
function createCode() {
	code = "";
	var codeLength = 4;// 验证码的长度
	var selectChar = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c',
			'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
			'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C',
			'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
			'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');// 所有候选组成验证码的字符，当然也可以用中文的
	for (var i = 0; i < codeLength; i++) {
		var charIndex = Math.floor(Math.random() * 62);
		code += selectChar[charIndex];
	}
	checkCodeField.setText(code);
}

var checkCodeField = new Ext.form.Label({
	name : 'checkCode',
	style : 'background-image: url("code.jpg"); font-family: Arial; font-style: italic; color: Red; border-width: 0px 0pt 0pt; border-style: none; border-color: -moz-use-text-color; padding: 0px 3px 2px; letter-spacing: 3px; font-weight: bolder; width: 152px;margin-left: 55px;',
	border : true,
	width : 80
});


var inputCodeField = new Ext.form.TextField({
			name : 'randCode',
			id : 'randCode',
			fieldLabel : '验证码',
			blankText : '验证码不能为空'
		});
var failMessage;
var submitFunction = function() {
	var inputCode = inputCodeField.getValue().toLowerCase();
	if (inputCode.length <= 0) {
		Ext.Msg.alert("系统信息：","请输入验证码！");
		return  0;
	} else if (inputCode != code.toLowerCase()) {
		Ext.Msg.alert("系统信息：","验证码输入错误！");
		createCode();
		return 0;
	}
	return 1;
}

Ext.onReady(function() {
			createCode();
			failMessage="";

			if(failMessageBoolean==1){
				failMessage ="用户名不存在或者密码错误！请重新登陆。";
			}
			
			var loginForm = new Ext.form.FormPanel({
						width : 500,
						height: 300,
						onSubmit : Ext.emptyFn,
						buttons : [{
							width:70,
						    style:'margin-left:35%; width: 70px; margin-top: 3%;',
						    iconCls : 'ok',
							xtype:'button',
							text:"超级管理员登录",
							handler : function(){
								var m = submitFunction();
								if(m == 0)
									return ;
								var form = loginForm.getForm().getEl().dom;  
						        form.action = "system/adminLogin.action";  
						        form.submit();  
						}
						},{
							width:70,
						    style:'margin-left:35%; width: 70px; margin-top: 3%;',
						    iconCls : 'ok',
							xtype:'button',
							text:"普通管理员登录",
							handler : function(){
							
								var m = submitFunction();
								if(m == 0)
									return ;
								var form = loginForm.getForm().getEl().dom;  
								form.action = "system/managerLogin.action";  
								form.submit();  
					        }
						},{
							width:70,
						    style:'margin-left:35%; width: 70px; margin-top: 3%;',
						    iconCls : 'ok',
							xtype:'button',
							text:"用户登录",
							handler : function(){
								var m = submitFunction();
								if(m == 0)
									return ;
								alert('1');
								var form = loginForm.getForm().getEl().dom;  
								alert('2');
								form.action = "system/userLogin.action";  
								alert('3');
								form.submit();  
				        }
						}],
						id : 'loginFormId',
						bodyStyle : 'padding-top:15px',
						renderTo : 'loginForm',
						defaultType : 'textfield',
						labelAlign : 'right',
						labelWidth : 55,
						labelPad : 2,
						frame : false,
						method : 'POST',
						buttonAlign : 'center',
						defaults : {
							allowBlank : true
						},
						items : [{
							style:'margin-left:10px;',
							xtype : 'label',
							text:'系统登录:'
						},{
									xtype : 'label',
									text:failMessage
								},
									{
									style : 'margin-top:20px;margin-bottom: 8px;',	
									labelStyle:'margin-top: 20px; margin-left: -45px;',
									name : 'pageMessage1',
									fieldLabel : '帐 号',
									blankText : '帐号不能为空'
								}, {
									style : 'margin-bottom: 8px;',	
									name : 'pageMessage2',
									fieldLabel : '密 码',
									blankText : '密码不能为空',
									inputType : 'password'
								}, inputCodeField, checkCodeField]
					});

		});
